package com.shaonianyou.util;

/**
 * 
 * @author Empty
 *
 */
public final class Pagination {

	/** 当前页数 */
	int pageIndex = 1;

	/** 每页显示记录数 */
	int pageSize = 10;

	/** 总记录数 */
	int totalRecord = 0;

	/** 总页数 */
	int totalPage = 1;

	public static final String PAGINATION_ATTR_KEY = "pagination";

	private Pagination(int pageIndex) {
		if (pageIndex >= 1) {
			this.pageIndex = pageIndex;
		}
	}

	private Pagination(int pageIndex, int pageSize) {
		if (pageIndex >= 1) {
			this.pageIndex = pageIndex;
		}
		if (pageSize >= 1) {
			this.pageSize = pageSize;
		}
	}

	/**
	 * @return the pageIndex
	 */
	public int getPageIndex() {
		return pageIndex;
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	public static Pagination getInstance(int pageIndex) {
		return new Pagination(pageIndex);
	}

	public static Pagination getInstance(int pageIndex, int pageSize) {
		return new Pagination(pageIndex, pageSize);
	}

	/** 返回开始记录数 */
	public int getStart() {
		return (pageIndex - 1) * pageSize;
	}

	/** 返回没页显示条数 */
	public int getLength() {
		return pageSize;
	}

	/**设置总记录数*/
	public void setTotalRecord(Integer totalRecord) {
		if(totalRecord != null){
			this.totalRecord = totalRecord;			
		}
	}

	/** 返回总页数 */
	public int getTotalPage() {

		if (totalRecord % pageSize == 0) {
			totalPage = totalRecord / pageSize;
		} else {
			totalPage = totalRecord / pageSize + 1;
		}
		return totalPage;
	}

}
